package fem2.element;

import inf.jlinalg.Array2DMatrix;
import inf.jlinalg.IMatrix;
import junit.framework.Assert;
import math2.MathUtilities;

import org.junit.Test;

import fem2.Debugger;
import fem2.Element;
import fem2.Face;
import fem2.MaterialModel;
import fem2.Node;
import fem2.enu.MassMatrixType;
import fem2.enu.State;
import fem2.material.StVenantKirchhoffMM;

public class StructuralElementTest {

	public Element createElement() {
		Node n1 = new Node(0, 0);
		Node n2 = new Node(3.5, 1);
		Node n3 = new Node(2.3, 2);
		Node n4 = new Node(-1.8, 0.5);
		Face f = new Face(n1, n2, n3, n4);

		double E = 1000;
		double nu = 0.3;
		double thickness = 1.0;
		double rho = 1.0;
		State ss = State.PLANE_STRESS;

		MaterialModel mm = new StVenantKirchhoffMM(E, nu, thickness, rho, ss);

		return new StructuralElement(f, mm);
	}

	@Test
	public void testMe() {
		Element e = createElement();
		int n = 2 * e.getMeshPart().countModes();

		IMatrix me = new Array2DMatrix(n, n);

		e.computeMe(me, MassMatrixType.VARIATIONAL_MASS_LUMPING);

		Debugger.watch("me = ", me);

		double[][] sampleHofemMe = new double[][] {
				{ 4.597222E-01, 0.00000E00, 2.458333E-01, 0.00000E00, 1.31250E-01, 0.00000E00,
						2.465278E-01, 0.00000E00 },
				{ 0.00000E00, 4.597222E-01, 0.00000E00, 2.458333E-01, 0.00000E00, 1.31250E-01,
						0.00000E00, 2.465278E-01 },
				{ 2.458333E-01, 0.00000E00, 5.236111E-01, 0.00000E00, 2.784722E-01, 0.00000E00,
						1.31250E-01, 0.00000E00 },
				{ 0.00000E00, 2.458333E-01, 0.00000E00, 5.236111E-01, 0.00000E00, 2.784722E-01,
						0.00000E00, 1.31250E-01 },
				{ 1.31250E-01, 0.00000E00, 2.784722E-01, 0.00000E00, 5.902778E-01, 0.00000E00,
						2.791667E-01, 0.00000E00 },
				{ 0.00000E00, 1.31250E-01, 0.00000E00, 2.784722E-01, 0.00000E00, 5.902778E-01,
						0.00000E00, 2.791667E-01 },
				{ 2.465278E-01, 0.00000E00, 1.31250E-01, 0.00000E00, 2.791667E-01, 0.00000E00,
						5.263889E-01, 0.00000E00 },
				{ 0.00000E00, 2.465278E-01, 0.00000E00, 1.31250E-01, 0.00000E00, 2.791667E-01,
						0.00000E00, 5.263889E-01 } };

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				Assert.assertEquals(sampleHofemMe[i][j], me.get(i, j), MathUtilities.tol6);
			}
		}
	}
}
